<%--
  Created by IntelliJ IDEA.
  User: 王旭东
  Date: 2024/12/16
  Time: 21:15
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>宠物领养管理后台</title>
    <link href="${pageContext.request.contextPath}/static/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/css/admin/metisMenu.min.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/css/admin/dataTables.bootstrap.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/css/admin/sb-admin-2.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/css/admin/font-awesome.min.css" rel="stylesheet" type="text/css">
    <link href="${pageContext.request.contextPath}/static/css/admin/boot-crm.css" rel="stylesheet" type="text/css">
    <style>
        .panel-heading{background-color: #337ab7;border-color: #2e6da4;font-size:14px;padding-left:20px;height:36px;line-height:36px;color:white;position:relative;cursor:pointer;}/*转成手形图标*/
        .panel-heading span{position:absolute;right:10px;top:12px;}
        table{
            border-bottom: 1px solid #ddd !important;
        }
    </style>
</head>
<body>
<div id="wrapper">
    <!-- 导航栏部分 -->
    <nav class="navbar navbar-default navbar-static-top" role="navigation" style="margin-bottom: 0">
        <div class="navbar-header">
            <a class="navbar-brand" href="">流浪猫狗管理系统</a>
        </div>
        <!-- 导航栏右侧图标部分 -->
        <!-- 导航栏右侧图标部分 -->
        <ul class="nav navbar-top-links navbar-right">
            <!-- 消息通知 end -->
            <!-- 用户信息和系统设置 start -->
            <li class="dropdown">
                <a class="dropdown-toggle" data-toggle="dropdown" href="/admin/developing">
                    <i class="fa fa-user fa-fw"></i>
                    <i class="fa fa-caret-down"></i>
                </a>
                <ul class="dropdown-menu dropdown-user ">
                    <li>
                        <input type="hidden" id = "currentAdminId" value="${admin.id}">
                    </li>
                    <li><a href="/admin/developing"><i class="fa fa-user fa-fw"></i>
                        管理员：${admin.adminName}</a>
                    </li>
                    <li><a href="/admin/developing"><i class="fa fa-gear fa-fw"></i> 系统设置</a></li>
                    <li class="divider"></li>
                    <li>
                        <a href="${pageContext.request.contextPath}/admin/AdminLogin">
                            <i class="fa fa-sign-out fa-fw"></i>退出登录
                        </a>
                    </li>
                </ul>
            </li>
            <!-- 用户信息和系统设置结束 -->
        </ul> <!-- 左侧显示列表部分 start-->
        <div class="navbar-default sidebar" role="navigation">
            <div class="sidebar-nav navbar-collapse">
                <!-- 教学管理  折叠的分组列表 -->
                <div class="panel-heading" id="collapseListGroupHeading3" data-toggle="collapse" data-target="#collapseListGroup3" role="tab">
                    <h4 class="panel-title">
                        后台管理 <span class="fa fa-chevron-up right"></span>
                    </h4>
                </div>
                <div id="collapseListGroup3" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="collapseListGroupHeading3">
                    <ul class="list-group">
                        <li class="list-group-item my_font">
                            <a href="${pageContext.request.contextPath}/admin/UserList">
                                <i class="fa fa-flash fa-fw"></i> 用户信息
                            </a>
                        </li>
                        <li class="list-group-item my_font">
                            <a href="${pageContext.request.contextPath}/admin/AdminsList">
                                <i class="fa fa-flash fa-fw"></i> 管理员信息
                            </a>
                        </li>
                        <li class="list-group-item my_font">
                            <a href="${pageContext.request.contextPath}/admin/PetList">
                                <i class="fa fa-sitemap fa-fw"></i> 宠物管理
                            </a>
                        </li>
                        <li class="list-group-item my_font">
                            <a href="${pageContext.request.contextPath}/admin/AdoptList">
                                <i class="fa fa-sitemap fa-fw"></i> 领养管理
                            </a>
                        </li>
                        <li class="list-group-item my_font">
                            <a href="${pageContext.request.contextPath}/admin/CommentList">
                                <i class="fa fa-sitemap fa-fw"></i> 评论管理
                            </a>
                        </li>
                        <li class="list-group-item my_font">
                            <a href="${pageContext.request.contextPath}/admin/BlogListService">
                                <i class="fa fa-sitemap fa-fw"></i> 团队活动管理
                            </a>
                        </li>
                        <li class="list-group-item my_font">
                            <a href="${pageContext.request.contextPath}/admin/ApplyList">
                                <i class="fa fa-sitemap fa-fw"></i> 志愿者的申请
                            </a>
                        </li>
                        <li class="list-group-item my_font">
                            <a href="${pageContext.request.contextPath}/admin/agreeList">
                                <i class="fa fa-sitemap fa-fw"></i> 同意领养列表
                            </a>
                        </li>
                        <li class="list-group-item my_font">
                            <a href="${pageContext.request.contextPath}/admin/disagreeList">
                                <i class="fa fa-sitemap fa-fw"></i> 不同意领养列表
                            </a>
                        </li>
                    </ul>
                </div>
            </div>
        </div><!-- 左侧显示列表部分 end-->

    </nav>
    <!-- 领养表列表查询部分  start-->
    <div id="page-wrapper">
        <div class="row">
            <div class="col-lg-12">
                <h1 class="page-header">猫狗领养申请的审核</h1>
            </div>
            <!-- /.col-lg-12 -->
        </div>
        <!-- /.row -->
        <div class="panel panel-default">
            <!-- 搜索部分 -->
            <div class="panel-body">
                <form class="form-inline" method="get" action="">
                    <div class="form-group">
                        <label for="findByAdoptTime">领养的时间</label>
                        <input type="date" class="form-control" id="findByAdoptTime" value="" name="adoptTime">
                    </div>
                    <button type="button" class="btn btn-primary" id="adopt_find_modal_btn">查询</button>
                </form>
            </div>
        </div>
        <div class="row">
            <div class="col-lg-12">
                <div class="panel panel-default">
                    <div class="panel-heading">申请信息列表</div>
                    <!-- /.panel-heading -->
                    <table class="table table-bordered table-striped" id="adopt_table">
                        <thead>
                        <tr>
                            <%--复选框，因为没有做相关功能，就弃用了--%>
                            <%-- <th>
                                 <input type="checkbox" id="check_all"/>
                             </th>--%>
                            <th>编号</th>
                            <th>用户名称</th>
                            <th>宠物名字</th>
                            <th>申请时间</th>
                            <th>状态</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        <c:forEach items="${adoptList}" var="adopt">
                            <tr>
                                <td>${adopt.id}</td>
                                <td>${adopt.user.userName}</td>
                                <td>${adopt.pet.petName}</td>
                                <td>${adopt.adoptTime}</td>
                                <td>
                                    <c:choose>
                                        <c:when test="${adopt.state == 0}">待审核</c:when>
                                        <c:when test="${adopt.state == 1}">已同意</c:when>
                                        <c:when test="${adopt.state == 2}">已拒绝</c:when>
                                    </c:choose>
                                </td>
                                <td>
                                    <c:if test="${adopt.state == 0}">
                                        <button class="btn btn-success btn-sm agree-btn" data-id="${adopt.id}">同意</button>
                                        <button class="btn btn-danger btn-sm disagree-btn" data-id="${adopt.id}">拒绝</button>
                                    </c:if>
                                </td>
                            </tr>
                        </c:forEach>
                        </tbody>
                    </table>
                    <div class="row">
                        <!--分页文字信息  -->
                        <div class="col-md-8" id="page_info_area"></div>
                        <!-- 分页条信息 -->
                        <div class="col-md-4" id="page_nav_area">

                        </div>
                    </div>
                    <!-- /.panel-body -->
                </div>
                <!-- /.panel -->
            </div>
            <!-- /.col-lg-12 -->
        </div>
    </div>
    <!-- 用户查询  end-->
</div>

<!-- Modal -->
<div class="modal fade" id="adoptModal" tabindex="-1" role="dialog" aria-labelledby="adoptModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="adoptModalLabel">领养申请信息</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <table class="table table-bordered table-striped">
                    <thead>
                    <tr>
                        <th>编号</th>
                        <th>用户名称</th>
                        <th>宠物名字</th>
                        <th>申请时间</th>
                        <th>状态</th>
                    </tr>
                    </thead>
                    <tbody id="modalTableBody">
                    <!-- 表格内容将通过 JavaScript 动态插入 -->
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>


<!-- 引入js文件 -->
<!-- jQuery -->
<script src="${pageContext.request.contextPath}/static/js/jquery-3.4.1.min.js"></script>
<script src="${pageContext.request.contextPath}/static/bootstrap/js/bootstrap.min.js"></script>
<script src="${pageContext.request.contextPath}/static/js/admin/metisMenu.min.js"></script>
<script src="${pageContext.request.contextPath}/static/js/admin/jquery.dataTables.min.js"></script>
<script src="${pageContext.request.contextPath}/static/js/admin/dataTables.bootstrap.min.js"></script>
<script src="${pageContext.request.contextPath}/static/js/admin/sb-admin-2.js"></script>
<!-- 编写js代码 -->
<script>
    $(document).ready(function() {
        // 同意领养申请
        $('.agree-btn').click(function() {
            var adoptId = $(this).data('id');
            $.ajax({
                url: '${pageContext.request.contextPath}/admin/AdoptAgree',
                type: 'POST',
                data: { id: adoptId },
                success: function(response) {
                    try {

                        if(response.success) {
                            // 更新行的状态显示
                            $('tr[data-id="' + adoptId + '"] td:nth-child(5)').text('已同意');
                            // 禁用按钮，防止重复点击
                            $('tr[data-id="' + adoptId + '"] button').attr('disabled', 'disabled');
                            // 重载页面以刷新状态
                            location.href="${pageContext.request.contextPath}/admin/AdoptList";
                        } else {
                            alert(response.error);
                        }
                    } catch (e) {
                        console.error("解析JSON失败：", e);
                        alert("解析JSON失败，请检查服务器响应。");
                    }
                },
                error: function(xhr, status, error) {
                    console.error("请求失败：", status, error);
                    alert('请求失败，请重试。');
                }
            });
        });

        // 拒绝领养申请
        $('.disagree-btn').click(function() {
            var adoptId = $(this).data('id');
            $.ajax({
                url: '${pageContext.request.contextPath}/admin/Adoptdisagree',
                type: 'POST',
                data: { id: adoptId },
                success: function(response) {
                    try {

                        if(response.success) {
                            // 更新行的状态显示
                            $('tr[data-id="' + adoptId + '"] td:nth-child(5)').text('已拒绝');
                            // 禁用按钮，防止重复点击
                            $('tr[data-id="' + adoptId + '"] button').attr('disabled', 'disabled');
                            // 重载页面以刷新状态
                            location.href="${pageContext.request.contextPath}/admin/AdoptList";
                        } else {
                            alert(response.error);
                        }
                    } catch (e) {
                        console.error("解析JSON失败：", e);
                        alert("解析JSON失败，请检查服务器响应。");
                    }
                },
                error: function(xhr, status, error) {
                    console.error("请求失败：", status, error);
                    alert('请求失败，请重试。');
                }
            });
        });
    })
    // 查询领养申请
    $('#adopt_find_modal_btn').click(function(event) {
        event.preventDefault(); // 阻止默认行为

        var adoptTime = $('#findByAdoptTime').val(); // 获取输入的领养时间

        if (adoptTime.trim() === "") {
            alert("请选择领养时间");
            return;
        }

        // 发送 AJAX 请求
        $.ajax({
            url: "${pageContext.request.contextPath}/admin/AdoptFindByTime",
            type: "GET",
            data: { adoptTime: adoptTime },
            success: function(response) {
                // 假设后端返回的是 JSON 格式的领养列表
                if (response.success) {
                    var resultHtml = '';
                    $.each(response.adoptList, function(index, adopt) {
                        resultHtml += '<tr>';
                        resultHtml += '<td>' + adopt.id + '</td>';
                        resultHtml += '<td>' + adopt.userName + '</td>';
                        resultHtml += '<td>' + adopt.petName + '</td>';
                        resultHtml += '<td>' + adopt.adoptTime + '</td>';
                        resultHtml += '<td>' + (adopt.state === 0 ? '待审核' : adopt.state === 1 ? '已同意' : '已拒绝') + '</td>';
                        resultHtml += '<td><button class="btn btn-success btn-sm">同意</button><button class="btn btn-danger btn-sm">拒绝</button></td>'; // 根据需要添加操作按钮
                        resultHtml += '</tr>';
                    });
                    $('#modalTableBody').html(resultHtml); // 更新模态框的表格内容
                    $('#adoptModal').modal('show'); // 显示模态框
                } else {
                    $('#modalTableBody').html('<tr><td colspan="6">未找到匹配的领养申请</td></tr>');
                }
            },
            error: function() {
                alert("查询失败，请稍后再试");
            }
        });
    });
</script>
</body>
</html>
